home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * uucplock.c, uubatch version 1.0.5 :
- *
- * This code is a modified version of the bsd tip uucplock.c source.
- * I got this source from UUNET and modified it a bit, making it
- * a complete executable program that locks/unlocks an "object".
- *
- * Below is the original Copyright found in the original file :
- *
- */
-
- /*
- * Copyright (c) 1988 The Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms are permitted
- * provided that: (1) source distributions retain this entire copyright
- * notice and comment, and (2) distributions including binaries display
- * the following acknowledgement: ``This product includes software
- * developed by the University of California, Berkeley and its contributors''
- * in the documentation or other materials provided with the distribution
- * and in all advertising materials mentioning features or use of this
- * software. Neither the name of the University nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- */
-
- #include <sys/types.h>
- #include <sys/file.h>
- #include <sys/dir.h>
- #include <errno.h>
- #ifndef NOHDB
- #include <stdio.h>
- #endif
-
- #ifdef SCO
- #include <limits.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #define PMAX _POSIX_PATH_MAX
- #define SetPosition SEEK_SET
- #define index(_a,_b) strchr(_a,_b)
- #else
- #define PMAX MAXNAMLEN
- #define SetPosition L_SET
- #endif
-
- #include "patchlevel.h"
-
- /*
- * uucp style locking routines
- * return: 0 - success
- * -1 - failure
- */
-
-
- uu_lock(objname)
- char *objname;
- {
- extern int errno;
- int fd, pid;
- char spid[256];
- char tbuf[sizeof(_PATH_LOCKDIRNAME) + PMAX];
- off_t lseek();
- #ifndef NOHDB
- FILE *ffd;
- #endif
-
- (void)sprintf(tbuf, _PATH_LOCKDIRNAME, objname);
- fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0666);
- if (fd < 0) {
- /*
- * file is already locked
- * check to see if the process holding the lock still exists
- */
- fd = open(tbuf, O_RDWR, 0);
- if (fd < 0) {
- perror("lock open");
- return(-1);
- }
- #ifdef NOHDB
- /* this is old BSD code, it reads the number in binary */
- /* form. This does not go well with HDB... */
- if (read(fd, &pid, sizeof(pid)) != sizeof(pid)) {
- #else
- /* This is code for HDB UUCP, it puts the pid in as text : */
- if (!(ffd=fdopen(fd,"r+"))) {
- perror("fdopen : lock open");
- return(-1);
- }
- if (!fscanf(ffd,"%d",&pid)) {
- #endif
- (void)close(fd);
- perror("lock read");
- return(-1);
- }
-
- if (kill(pid, 0) == 0 || errno != ESRCH) {
- (void)close(fd); /* process is still running */
- return(-1);
- }
- /*
- * The process that locked the file isn't running, so
- * we'll lock it ourselves
- */
- if (lseek(fd, 0L, SetPosition) < 0) {
- (void)close(fd);
- perror("lock lseek");
- return(-1);
- }
- /* fall out and finish the locking process */
- }
-
- pid = getppid(); /* Parent process is the shell */
- #ifdef NOHDB
- /* this is old BSD lock code, it writes the number in in binary */
- /* form. This does not go well with HDB... */
- if (write(fd, (char *)&pid, sizeof(pid)) != sizeof(pid)) {
- (void)close(fd);
- (void)unlink(tbuf);
- perror("lock write");
- return(-1);
- }
- #else
- /* This is for HDB UUCP : it puts the pid in as text... */
- sprintf(spid,"%10d\n",pid);
- if (write(fd, spid, strlen(spid)) != strlen(spid)) {
- (void)close(fd);
- (void)unlink(tbuf);
- perror("lock write");
- return(-1);
- }
- #endif
- (void)close(fd);
- return(0);
- }
-
- uu_unlock(objname)
- char *objname;
- {
- char tbuf[sizeof(_PATH_LOCKDIRNAME) + PMAX];
-
- (void)sprintf(tbuf, _PATH_LOCKDIRNAME, objname);
- return(unlink(tbuf));
- }
-
- p_usage(s)
- char *s;
- {
- fprintf(stderr," FAILED. Wrong usage.\n");
- fprintf(stderr," Usage : %s lock|unlock objname\n");
- fprintf(stderr," Will return SUCCES or FAILED string.\n");
- exit(-1);
- }
-
- main(argc,argv)
- int argc;
- char **argv;
- {
-
- int failed;
- if (argc!=3)
- p_usage(argv[0]);
-
- if (!strcmp(argv[1],"lock"))
- {
- fprintf(stderr,"lock : %s\n",
- (failed=uu_lock(argv[2])) ? "FAILED" : "SUCCESS" );
- exit(failed ? 1 : 0);
- }
- if (!strcmp(argv[1],"unlock"))
- {
- fprintf(stderr,"unlock : %s\n",
- (failed=uu_unlock(argv[2])) ? "FAILED" : "SUCCESS" );
- exit(failed ? 1 : 0);
- }
-
- /* If we get here then wrong usage : */
- p_usage(argv[0]);
- }
-